//
//  ReadViewController.m
//  WWLNetease
//
//  Created by 李锦耀 on 15/12/22.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "ReadViewController.h"
#import "LJYaoRecommendViewController.h"
#import "LJYaoMineViewController.h"


@interface ReadViewController ()<UIScrollViewDelegate>


@property (nonatomic ,strong)UIScrollView *myScrollerView;
@property (nonatomic ,strong)NSArray *titleArray;

@end

@implementation ReadViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeFrame" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeFrameBack" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor orangeColor];
    _titleArray = @[@"推荐阅读",@"我的订阅"];
    _segementC = [[UISegmentedControl alloc] initWithItems:_titleArray];
    [self.navigationController.navigationBar addSubview:_segementC];
    _segementC.frame = CGRectMake(SPACE, self.navigationController.navigationBar.frame.origin.y-20, SCREEN_WIDTH.size.width-2*SPACE, 36);
    _segementC.layer.masksToBounds = YES;
    _segementC.layer.cornerRadius = 12;
    _segementC.layer.borderWidth = 2;
    _segementC.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor whiteColor]);
    _segementC.tintColor = [UIColor whiteColor];
//    [_segementC contentOffsetForSegmentAtIndex:0];
    self.segementC.selectedSegmentIndex = 0;
    [self.segementC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//    [_segementC setImage:[UIImage imageNamed:@"channel_edit_button_selected_bg@2x.png"]forSegmentAtIndex:1];
//    [_segementC setTitle:@"我的订阅" forSegmentAtIndex:1];
//    [_segementC mas_makeConstraints:^(MASConstraintMaker *make) {
//       
//        make.bottom.equalTo(self.navigationController.navigationBar.mas_bottom).offset(8);
//        make.left.mas_equalTo(SPACE);
//        make.right.mas_equalTo(SPACE);
//        make.top.equalTo(self.navigationController.navigationBar.mas_top);
//    }];
    _myScrollerView = [[UIScrollView alloc] init];
    _myScrollerView.frame = [[UIScreen mainScreen] bounds];
    _myScrollerView.backgroundColor = [UIColor orangeColor];
    _myScrollerView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    _myScrollerView.showsHorizontalScrollIndicator = NO;
    _myScrollerView.pagingEnabled = YES;
    _myScrollerView.delegate = self;
    _myScrollerView.bounces = NO;
    [self.view addSubview:self.myScrollerView];
    
    LJYaoRecommendViewController *firstVC = [[LJYaoRecommendViewController alloc] init];
    [self addChildViewController:firstVC];
    [self.myScrollerView addSubview:firstVC.view];
    
    LJYaoMineViewController *secondVC = [[LJYaoMineViewController alloc] init];
    [self addChildViewController:secondVC];
    CGRect secondSize = secondVC.view.frame;
    secondSize.origin.x = [[UIScreen mainScreen] bounds].size.width;
    secondVC.view.frame = secondSize;
    [self.myScrollerView addSubview:secondVC.view];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFrame:) name:@"changeFrame" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFrameBack:) name:@"changeFrameBack" object:nil];
}

-(void)changeFrame:(NSNotification *)notifition
{
    self.segementC.hidden = YES;
}

-(void)changeFrameBack:(NSNotification *)notifition
{
    self.segementC.hidden = NO;
}


- (void)segmentAction:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {
        
        self.myScrollerView.contentOffset = CGPointMake(0, 0);
    }else if (segment.selectedSegmentIndex == 1){
        
        self.myScrollerView.contentOffset = CGPointMake(SCREEN_WIDTH.size.width, 0);
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.myScrollerView.contentOffset.x == 0) {
        
        self.segementC.selectedSegmentIndex = 0;
    }else if(self.myScrollerView.contentOffset.x == SCREEN_WIDTH.size.width){
        
        self.segementC.selectedSegmentIndex = 1;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
